#导包
import json
from pyecharts.charts import Line
from pyecharts.options import TitleOpts,LabelOpts

#去不规范开头和结尾存放到新文件中
def tmp_file(old_file,new_file,cat_head,cat_tail):
    """
    :param old_file: 原文件
    :param new_file: 规范后存放的新文件
    :return: 返回规范数据
    """
    file_data = open(old_file, "r", encoding="UTF-8")
    data = file_data.read()
    file_data.close()
    data = data[cat_head:cat_tail]
    file_data = open(new_file, "w", encoding="UTF-8")
    file_data.write(data)
    file_data.close()
    return data

#调用规范
us_data = tmp_file("D:/me/study/python/execise/开发实例/数据可视化/data/美国.txt","D:/me/study/python/execise/开发实例/数据可视化/data/美国_tmp.txt",26,-2)
jp_data = tmp_file("D:/me/study/python/execise/开发实例/数据可视化/data/日本.txt","D:/me/study/python/execise/开发实例/数据可视化/data/日本_tmp.txt",26,-2)
in_data = tmp_file("D:/me/study/python/execise/开发实例/数据可视化/data/印度.txt","D:/me/study/python/execise/开发实例/数据可视化/data/印度_tmp.txt",26,-2)

#json转python
us_dict = json.loads(us_data)
jp_dict = json.loads(jp_data)
in_dict = json.loads(in_data)
#截取trend字典
us_trend = us_dict['data'][0]['trend']
jp_trend = jp_dict['data'][0]['trend']
in_trend = in_dict['data'][0]['trend']
#截取时间到下标314给x轴变量
us_x_date = us_trend['updateDate'][:314]
jp_x_date = jp_trend['updateDate'][:314]
in_x_date = in_trend['updateDate'][:314]
#截取确诊数据到下标314给y轴变量
us_y_data_quezhen = us_trend['list'][0]['data'][:314]
jp_y_data_quezhen = jp_trend['list'][0]['data'][:314]
in_y_data_quezhen = in_trend['list'][0]['data'][:314]

#构建图表
line = Line()
#构图全局配置
line.set_global_opts(
    title_opts=TitleOpts(title="2020年疫情确诊人数",pos_left="center",pos_bottom="1%"),  #标题展示
)
#构建折线图
line.add_xaxis(us_x_date)   #时间
line.add_yaxis("美国确诊人数",us_y_data_quezhen,label_opts=LabelOpts(is_show=False))  #确诊人数
line.add_yaxis("日本确诊人数",jp_y_data_quezhen,label_opts=LabelOpts(is_show=False))  #确诊人数
line.add_yaxis("印度确诊人数",in_y_data_quezhen,label_opts=LabelOpts(is_show=False))  #确诊人数
line.render()